package com.ssps.back.platform.controller;

import com.ssps.back.platform.service.UserInfoService;
import com.ssps.back.platform.service.UserServcie;
import com.ssps.entity.Permission;
import com.ssps.entity.User;
import com.ssps.entity.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

/**
 * @author li
 * @time 2020/07/21 20:55:05
 */

@Controller
public class LoginController {

    @Autowired
    UserServcie userServcie;
    @Autowired
    UserInfoService userInfoService;

    @RequestMapping("/")
    public String home(){
        return "user/login";
    }

    @RequestMapping("menu")
    public String mrnu(){
        return "menu";
    }
// 0

    @RequestMapping("unauthorized")
    public String unauthorized(){
        return "unauthorized";
    }

    @RequestMapping("updatePwd")
    public String updatePwd(){
        return "user/updatePwd";
    }



    @RequestMapping("login")
    public String Login(String phone,String pwd,HttpSession session){
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(phone,pwd);
        String path=null;
        User user=userServcie.FindOneUser(phone);

        List<Permission> list = userServcie.getTrees(user);
        System.out.println(list);
        UserInfo userInfo = userInfoService.finOne(user.getUid());
        session.setAttribute("trees",list);
        session.setAttribute("loginUser",user);
        session.setAttribute("loginUserInfo",userInfo);
        try {
            subject.login(token);
            path = "/user/userList";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            path = "/user/login";
        }
        return path;

    }


    @RequestMapping("exit")
    public String loginout() {
        return "logout";
    }


}
